package com.iot.web.controller.device;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.iot.mqtt.callback.PressureSensorClientMqttCallback;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.iot.common.annotation.Log;
import com.iot.common.core.controller.BaseController;
import com.iot.common.core.domain.AjaxResult;
import com.iot.common.enums.BusinessType;
import com.iot.device.domain.PressureSensorAlarmDevice;
import com.iot.device.service.IPressureSensorAlarmDeviceService;
import com.iot.common.utils.poi.ExcelUtil;
import com.iot.common.core.page.TableDataInfo;

/**
 * 压力报警灯Controller
 *
 * @author iot
 * @date 2023-08-07
 */
@RestController
@RequestMapping("/device/pressureSensorAlarmDevice")
public class PressureSensorAlarmDeviceController extends BaseController
{
    @Autowired
    private IPressureSensorAlarmDeviceService pressureSensorAlarmDeviceService;
    @Autowired
    private PressureSensorClientMqttCallback pressureSensorClientMqttCallback;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 设置压力报警灯参数
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:setConfig')")
    @PostMapping("/setConfig/{deviceNo}")
    public AjaxResult setConfig(@PathVariable("deviceNo") String deviceNo, PressureSensorAlarmDevice pressureSensorAlarmDevice)
    {
        /*
            {
                "todo": "setConfig",
                "alarmDeviceNo": "0224310202307102,0224310202307103",
                "alarmTime": "2",
                "longitude": "118.091401",
                "latitude": "39.582459",
                "volumeLevel": "7",
                "ntpServer": "ntp1.aliyun.com",
                "deviceMagnitude": "4.0",
                "configIntensity": "3",
                "centerName": "shandong",
                "earthquakeOpenOrnot": "true",
                "alarmDeviceSendTopic": "/alarmSend/set",
                "alarmDeviceGetTopic": "/alarmGet/get",
                "sendToCloudTopic": "/sendCloud/set",
                "getFromCloudTopic": "/getCloud/get"
            }
        */
        JSONObject param = JSONObject.parseObject(JSONObject.toJSONString(pressureSensorAlarmDevice));
        param.put("todo", "setConfig");
        String topic = PressureSensorClientMqttCallback.SENDCLOUD_GET.replace("${deviceNo}", deviceNo);
        pressureSensorClientMqttCallback.publish(topic, 2, param.toJSONString());
        return AjaxResult.success();
    }

    /**
     * 发布演练
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:earthquakeDrill')")
    @GetMapping("/earthquakeDrill/{deviceNo}")
    public AjaxResult earthquakeDrill(@PathVariable("deviceNo") String deviceNo, @RequestParam("countDowns") Integer countDowns, @RequestParam("intensity") Integer intensity)
    {
        /*
            {
                "todo":"earthquakeWarningDrill",
                "msgCode":"0101",
                "countDowns":"20",
                "Intensity":"5"
            }
        */
        JSONObject param = new JSONObject();
        param.put("todo", "earthquakeWarningDrill");
        param.put("msgCode", "0101");
        param.put("countDowns", countDowns);
        param.put("intensity", intensity);
        String topic = PressureSensorClientMqttCallback.SENDCLOUD_GET.replace("${deviceNo}", deviceNo);
        pressureSensorClientMqttCallback.publish(topic, 2, param.toJSONString());
        return AjaxResult.success();
    }

    /**
     * 压力报警灯重启
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:restart')")
    @PostMapping("/restart")
    public AjaxResult restart(@RequestParam("deviceNos") String deviceNos)
    {
        /*
            {“restart”:”restart”}
        */
        JSONObject param = new JSONObject();
        param.put("restart", "restart");
        threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                String[] deviceNoArr = deviceNos.split(",");
                for(String deviceNo : deviceNoArr){
                    String topic = PressureSensorClientMqttCallback.SENDCLOUD_GET.replace("${deviceNo}", deviceNo);
                    pressureSensorClientMqttCallback.publish(topic, 2, param.toJSONString());
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return AjaxResult.success();
    }

    /**
     * 查询压力报警灯列表
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:list')")
    @GetMapping("/list")
    public TableDataInfo list(PressureSensorAlarmDevice pressureSensorAlarmDevice)
    {
        startPage();
        List<PressureSensorAlarmDevice> list = pressureSensorAlarmDeviceService.selectPressureSensorAlarmDeviceList(pressureSensorAlarmDevice);
        return getDataTable(list);
    }

    /**
     * 导出压力报警灯列表
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:export')")
    @Log(title = "压力报警灯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PressureSensorAlarmDevice pressureSensorAlarmDevice)
    {
        List<PressureSensorAlarmDevice> list = pressureSensorAlarmDeviceService.selectPressureSensorAlarmDeviceList(pressureSensorAlarmDevice);
        ExcelUtil<PressureSensorAlarmDevice> util = new ExcelUtil<PressureSensorAlarmDevice>(PressureSensorAlarmDevice.class);
        util.exportExcel(response, list, "压力报警灯数据");
    }

    /**
     * 获取压力报警灯详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pressureSensorAlarmDeviceService.selectPressureSensorAlarmDeviceById(id));
    }

    /**
     * 新增压力报警灯
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:add')")
    @Log(title = "压力报警灯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PressureSensorAlarmDevice pressureSensorAlarmDevice)
    {
        return toAjax(pressureSensorAlarmDeviceService.insertPressureSensorAlarmDevice(pressureSensorAlarmDevice));
    }

    /**
     * 修改压力报警灯
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:edit')")
    @Log(title = "压力报警灯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PressureSensorAlarmDevice pressureSensorAlarmDevice)
    {
        return toAjax(pressureSensorAlarmDeviceService.updatePressureSensorAlarmDevice(pressureSensorAlarmDevice));
    }

    /**
     * 删除压力报警灯
     */
    @PreAuthorize("@ss.hasPermi('device:pressureSensorAlarmDevice:remove')")
    @Log(title = "压力报警灯", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pressureSensorAlarmDeviceService.deletePressureSensorAlarmDeviceByIds(ids));
    }
}
